<template>
  <div class="app-container">
    <el-container style="height: 500px; border: 1px solid #eee">
<el-aside width="21%" style="background-color: white;">
<el-row>
  <el-col :span="20">
      <el-tabs v-model="activeName" tab-position="left" style="height: 200px;" @tab-click="setResType">
        <el-tab-pane label="所有文件" name="0"></el-tab-pane>
        <el-tab-pane :label="dict.label" v-for="dict in this.getDictDatas(DICT_TYPE.RES_TYPE)" :name="dict.value"></el-tab-pane>
      </el-tabs>
  </el-col>
</el-row>
</el-aside>
    <el-main width="79%">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资源名称" prop="resName">
        <el-input v-model="queryParams.resName" placeholder="请输入资源名称" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['biz:res-file:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['biz:res-file:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" type="index" width="50"/>
      <el-table-column label="资源名称" align="center" prop="resName" />
      <el-table-column label="附件名称" align="center">
        <template slot-scope="scope">
          <el-link @click="handleDownload(scope.row.ossId)" target="_self">{{scope.row.fileName}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="formatDate"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" :formatter="formatDate"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['biz:res-file:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['biz:res-file:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    </el-main>
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="资源名称" prop="resName">
          <el-input v-model="form.resName" placeholder="请输入资源名称" />
        </el-form-item>
        <el-form-item label="资源类型" prop="resType">
          <el-select v-model="form.resType" placeholder="请选择资源类型" disabled>
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.RES_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="请选择附件" prop="ossFile">
          <fileUpload v-model="form.ossFile" limit="1"/>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    </el-container>
  </div>
</template>

<script>
import { createResFile, updateResFile, deleteResFile, getResFile, getResFilePage, exportResFileExcel } from "@/api/biz/resFile";
import FileUpload from '@/components/FileUpload';
import { getDictDatas, DICT_TYPE } from '@/utils/dict';
import ElAside from "../../../../node_modules/element-ui/packages/aside/src/main.vue";
import ElMain from "../../../../node_modules/element-ui/packages/main/src/main.vue";
import ElButton from "../../../../node_modules/element-ui/packages/button/src/button.vue";
import ElLink from "../../../../node_modules/element-ui/packages/link/src/main.vue";

export default {
  name: "ResFile",
  components: {
    ElLink,
    ElButton,
    ElMain,
    ElAside,
    FileUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资源库文件列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //当前激活的资源库类型
      activeResType:null,
      //当前激活的tab 名称
      activeName:'0',
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        resName: null,
        resType:null,
//        fileName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        resName: [{ required: true, message: "资源名称不能为空", trigger: "blur" }],
        resType: [{ required: true, message: "资源类型不能为空", trigger: "change" }],
        ossFile: [{ required: true, message: "附件不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //时间格式化
    formatDate(row, column){
      let data = row[column.property];
      let dt=new Date(data);
      return dt.getFullYear()+"-"+(dt.getMonth()+1)+'-'+dt.getDate()+' '+dt.getHours()+':'+dt.getMinutes()+":"+dt.getSeconds();
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      // 执行查询
      getResFilePage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        resName: undefined,
        resType: undefined,
        ossId: undefined,
        fileName:undefined,
        fileUrl:undefined,
        note: undefined,
        deleted:0,
        ossFile:undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.queryParams.resType=this.activeResType;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      if(this.activeResType=='0'||this.activeResType==0){
        this.$modal.msgWarning("请先选择一个资源库再上传文件！");
        return;
      }
      this.form.resType=this.activeResType;
      this.open = true;
      this.title = "添加资源库文件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getResFile(id).then(response => {
        this.form = response.data;
//        let file=new Object({'name':this.form.fileName,'originalName':this.form.fileName,'url':this.form.fileUrl,'uid':new Number(this.form.ossId)});
//        this.form.ossFile=new Array(file);
        this.open = true;
        this.title = "修改资源库文件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          let arry=this.form.ossFile;
          this.form.ossId=arry[0].id;
          this.form.fileName=arry[0].originalName;
          this.form.fileUrl=arry[0].url;
          updateResFile(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.ossFile=null;
            this.getList();
          });
          return;
        }
        // 添加的提交
        let arry=this.form.ossFile;
        this.form.ossId=arry[0].id;
        this.form.fileName=arry[0].originalName;
        this.form.fileUrl=arry[0].url;
        createResFile(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除资源库名称为"' + row.resName + '"的数据项?').then(function() {
          return deleteResFile(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      // 执行导出
      this.$modal.confirm('是否确认导出所有资源库文件数据项?').then(() => {
          this.exportLoading = true;
          return exportResFileExcel(params);
        }).then(response => {
          this.$download.excel(response, '${table.classComment}.xls');
          this.exportLoading = false;
        }).catch(() => {});
    },
    setResType(tab,event){
      this.activeResType=this.activeName;
      this.handleQuery();
    },
    /** 下载文件 */
    handleDownload(id) {
      this.$downloadOss.oss(id)
    },
  }
};
</script>
